#include "jwt.h"

char *encode(char *JWT, char *user_id){
    char *jwt;
    size_t jwt_length;

    struct l8w8jwt_claim header_claims[] = {
        {
            .key = "user",
            .key_length = 4,
            .value = "some-key-id-here-012345",
            .value_length = strlen("some-key-id-here-012345"),            
            .type = L8W8JWT_CLAIM_TYPE_STRING,
        }
    };
    struct l8w8jwt_claim payload_claims[] =
    {
        {
            .key = "userid",
            .key_length = 6,
            .value = user_id,
            .value_length = strlen(user_id),
            .type = L8W8JWT_CLAIM_TYPE_STRING
        },

    };

    struct l8w8jwt_encoding_params params;
    l8w8jwt_encoding_params_init(&params);

    params.alg = L8W8JWT_ALG_ES256;

    params.sub = "login";
    params.sub_length = strlen("login");

    params.iss = "server";
    params.iss_length = strlen("server");

    params.aud = "Administrator";
    params.aud_length = strlen("Administrator");

    params.iat = time(NULL);
    params.exp = time(NULL) + 5;

    params.additional_header_claims = header_claims;
    params.additional_header_claims_count = sizeof(header_claims) / sizeof(struct l8w8jwt_claim);

    params.additional_payload_claims = payload_claims;
    params.additional_payload_claims_count = sizeof(payload_claims) / sizeof(struct l8w8jwt_claim);

    params.secret_key = (unsigned char*)ECDSA_PRIVATE_KEY;
    params.secret_key_length = strlen(ECDSA_PRIVATE_KEY);

    params.out = &jwt;
    params.out_length = &jwt_length;

    int r = l8w8jwt_encode(&params);
    //printf("\nl8w8jwt_encode_es256 function returned %s (code %d).\n\nCreated token: \n%s\n", r == L8W8JWT_SUCCESS ? "successfully" : "", r, jwt);
    strcpy(JWT, jwt);
    l8w8jwt_free(jwt); /* Never forget to free the jwt string! */
    return JWT;    
}
bool decode(char *JWT){
    struct l8w8jwt_decoding_params params;
    l8w8jwt_decoding_params_init(&params);

    params.alg = L8W8JWT_ALG_ES256;

    params.jwt = (char*)JWT;
    params.jwt_length = strlen(JWT);
    params.verification_key = (unsigned char*)ECDSA_PUBLIC_KEY;
    params.verification_key_length = strlen(ECDSA_PUBLIC_KEY);
    params.validate_iss = "login";
    params.validate_sub_length = strlen("login");

    params.validate_iss = "server";
    params.validate_iss_length = strlen("server");
    params.validate_exp = 1;
    params.exp_tolerance_seconds = 60;

    params.validate_iat = 1;
    params.iat_tolerance_seconds = 60;

    enum l8w8jwt_validation_result validation_result;
    int r = l8w8jwt_decode(&params, &validation_result, NULL, NULL);
    //printf("\nl8w8jwt_decode_es256 function returned %s (code %d).\n\nValidation result: \n%d\n", r == L8W8JWT_SUCCESS ? "successfully" : "", r, validation_result);
    int decode_result = l8w8jwt_decode(&params, &validation_result, NULL, NULL);

    if (r == L8W8JWT_SUCCESS && validation_result == L8W8JWT_VALID) 
    {
        printf("true!\n");
        return true;
    }
    else
    {
        printf("false!\n");
        return false;
    }
}
//int main(){
//    char JWT[512] = {0};
//    char user_id[100] = "1";
//    strcpy(JWT, encode(JWT, user_id));
//    decode(JWT);
//    sleep(10);
//    decode(JWT);
//}
